package com.atguigu.springboot.servlet;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.codehaus.groovy.util.StringUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Test {

    public static void main(String[] args) throws Exception {
        showExcel("D:\\1.xlsx");
    }
    /**
     * 解析xls和xlsx文件
     *
     * @param path 文件路径
     * @throws Exception
     */
    public static void showExcel(String path) throws Exception {
        List<List<Object>> list=new ArrayList<List<Object>>();
        String fileType = path.substring(path.lastIndexOf(".") + 1, path.length());
        Workbook wb = new XSSFWorkbook(new FileInputStream(path));;

        Sheet sheet = null;
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {// 获取每个Sheet表
            sheet = wb.getSheetAt(i);
            for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {// getLastRowNum，获取最后一行的行标
                Row row = sheet.getRow(j);
                if (row != null) {
                    List<Object> list1=new ArrayList<Object>();
                    for (int k = 0; k < row.getLastCellNum(); k++) {// getLastCellNum，是获取最后一个不为空的列是第几个
                        System.out.println("第"+k+"行");
                        if (row.getCell(k) != null) { // getCell 获取单元格数据
                            Cell cell=row.getCell(k);
                            cell.setCellType(1);
                            System.out.println(row.getCell(k) + "\t");
                           list1.add(cell.toString());
                 /*          */
                        } else {
                            list1.add("");
                        }
                    }
                    System.out.println("数组长度"+list1.size());
                }
                System.out.println(""); // 读完一行后换行
            }
        }
    }


}
